package net.chowda.castcluster.interceptor;

import net.sourceforge.stripes.controller.Intercepts;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.controller.Interceptor;
import net.sourceforge.stripes.controller.ExecutionContext;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.ActionBean;
import net.chowda.castcluster.util.CastClusterConfig;
import net.chowda.castcluster.action.ConfigAction;
import net.chowda.castcluster.action.ajax.EditConfigAction;

/**
 * we don't want to force the user to edit config files by hand!
 *
 * this class will be responsible for forcing the user to set any required config values
 * before using the application.
 */
@Intercepts(LifecycleStage.HandlerResolution)
public class ConfigCheckInterceptor implements Interceptor {
    public Resolution intercept(ExecutionContext executionContext) throws Exception {
        Class<? extends ActionBean> actionClass = executionContext.getActionBean().getClass();
        // we force the user to edit the config if they only have one prop (config_base_dir)
        if(CastClusterConfig.getInstance().getGlobalProp(CastClusterConfig.CONVERTER_PATH) == null
                && actionClass != ConfigAction.class && actionClass != EditConfigAction.class) {
            return new ForwardResolution("/Config.action");
        }

        return executionContext.proceed();
    }
}
